package edu.uc.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import edu.uc.service.IUserService;
import edu.uc.service.UserServiceStub;

/**
 * 
 * @author ckattick displays the login screen and handles the component
 *         listeners
 */
public class IntroScreen extends CeasBaseScreen {

	public void onCreateOptionsMenu(MenuItem menu) {

	}

	// declare local variables
	private EditText username;
	private EditText password;
	private String validUser;
	private String validPass;
	private Button login;
	private Button create;
	private TextView warning;
	private IUserService userService;

	// call activity
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		initComponents();
		// Instantiate UserServiceStub
		userService = new UserServiceStub();

	}

	private void initComponents() {

		username = (EditText) findViewById(R.id.userNametxt);
		password = (EditText) findViewById(R.id.passwordtxt);
		login = (Button) findViewById(R.id.loginbtn);
		create = (Button) findViewById(R.id.addaccountbtn);
		warning = (TextView) findViewById(R.id.warning);

		// userService = new UserServiceStub();
		login.setOnClickListener(new OnClickListener() {

			// @Override
			public void onClick(View v) {
				login();

			}
		});

		// declare create user button click event
		create.setOnClickListener(new OnClickListener() {
			// @Override
			public void onClick(View v) {
				createNewUser();

			}
		});
	}

	/**
	 * Verifies that fields are filled out and that user is correct user.
	 * Implements the loadLoggedInScreen method
	 */
	// method for login button when clicked
	public void login() {
		validUser = username.getText().toString();
		validPass = password.getText().toString();
		// validate username field is filled out
		if (username.getText().toString().length() == 0) {
			username.setError("Invalid Username");
		}
		// validate password field is filled out
		if (password.getText().toString().length() == 0) {
			password.setError("Invalid Password");
		}
		// continue login method

		try {
			if (userService.logon(validUser, validPass) == true) {
				loadLoggedInScreen();
			} else {
				warning.setText("Invalid Password" + validPass);
			}
		} catch (Exception e1) {
			warning.setText("Something went wrong with processing your request, please try again");

		}

	}

	// method for create user button when clicked. Should generate new screen
	public void createNewUser() {
		loadNewUserScreen();
	}

	private void loadLoggedInScreen() {
		// render the plant search screen.
		Intent loggedIn = new Intent(this, LoggedInScreen.class);
		startActivity(loggedIn);
	}

	private void loadNewUserScreen() {
		Intent newUser = new Intent(this, CreateNewUserScreen.class);
		startActivity(newUser);
	}

}
